import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import RandomUtil from '@/RandomUtil';
import SelectUtil from '@/SelectUtil';
import MovementCommon from 'code/Core/Common/MovementCommon';
import SLKCommon from 'code/Core/Common/SLKCommon';
import UnitCommon from 'code/Core/Common/UnitCommon';
import TalentBase from 'code/Scripts/Abilities/base/TalentBase';
const desc = `每次攻击有${ColorSafe}15/10/5%|r的机率额外攻击${ColorSafe}1/2/3|r次。|n觉醒效果：触发机率提升至${ColorSafe}22.5/15/7.5%|r`;
const art = 'ReplaceableTextures\\CommandButtons\\BTNTheBlackArrow.blp';
const name = '连珠箭';
const config: TalentConfigModel = {
    id: 'talent_t_TalentMultipleMissile',
    name,
    desc,
    art,
    buffType: {
        id: 'buff_t_TalentMultipleMissile',
        art,
        name,
        ubertip: desc,
    },
    buffTypePlus: {
        id: 'buff_t_TalentMultipleMissile_plus',
        art,
        name: '连珠箭[觉醒]',
        ubertip: `每次攻击有${ColorSafe}22.5/15/7.5%%|r的机率额外攻击${ColorSafe}1/2/3|r次`,
    },
};
export default class TalentMultipleMissile extends TalentBase {
    static instance: TalentMultipleMissile;
    onHit(p: TargetMissileConfig) {
        DamageSystemInstance.applyAttachAttackDamage(p.target, p.caster, UnitCommon.取攻击(p.caster));
    }
    constructor(p?: TalentConfigModel) {
        super(p);
        TalentMultipleMissile.instance = this;
        this.setTalentConfig(config);
        EventManager.onTakeAttack(this.attackEffect);
    }
    attackEffect(d: DamageEventData) {
        if (d.isAttach) return;
        let { unEffect, plus } = TalentMultipleMissile.instance.talentUnEffect(d.source);
        if (unEffect) return;
        let r = RandomUtil.randomPercent();
        let r1 = 0.15,
            r2 = 0.1,
            r3 = 0.3;
        if (plus) {
            r1 *= 1.5;
            r2 *= 1.5;
            r3 *= 1.5;
        }
        let count = 0;
        if (r < r3) {
            count = 3;
        } else if (r < r2) {
            count = 2;
        } else if (r < r1) {
            count = 1;
        }
        if (count <= 0) return;
        let { target, source } = d;
        BaseUtil.onTimer(0.3, (c) => {
            let tar: unit;
            if (UnitAlive(target)) {
                tar = target;
            } else {
                let es = SelectUtil.getEnemyUnitsInRange(source, 350, GetUnitX(target), GetUnitY(target));
                if (es.length > 0) {
                    tar = ArrayUtil.randomElement(es);
                } else {
                    return false;
                }
            }
            MovementCommon.TargetMissile({
                missile: SLKCommon.getUnitMissileart(source),
                caster: source,
                target: tar,
                speed: SLKCommon.getUnitMissileSpeed(source),
                radian: SLKCommon.getUnitMissilearc(source),
                onReach: TalentMultipleMissile.instance.onHit,
            });
            return c < count;
        });
    }
}
